/*******************************************************************************
 * Copyright (c) Jerges A. Bello G. 2014. Reservado todos los derechos.
 ******************************************************************************/
package com.jabex.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.jabex.common.exceptions.ErrorFunctionalException;
import com.jabex.common.exceptions.ErrorOperationalException;
import com.jabex.common.exceptions.WarningFunctionalException;
import com.jabex.common.to.MessageTO;
import com.jabex.common.util.Constants;
import com.jabex.persistence.model.User;

/**
 * Created by Jerges Bello on 14/05/2014.
 */
@Service(value = IUserDAO.MODEL_NAME)
@Scope("request")
public class UserDAO extends AbstractDAO<User> implements IUserDAO {


	@Override
	public MessageTO insertUser(User user) {
		String result = persist(user);
		MessageTO messageTO = new MessageTO();
		if (result.isEmpty()) {
			messageTO.setCode(Constants.CODE_ERROR_OPERATIONAL);
			messageTO.setMessage(Constants.OPERATION_NOK);
			messageTO.setIsError(true);
		}
		return messageTO;
	}

	@Override
	public MessageTO deleteUser(String id) throws HibernateException {
		MessageTO messageTO = new MessageTO();
		if (!delete(getUserById(id))) {
			messageTO.setCode(Constants.CODE_ERROR_OPERATIONAL);
			messageTO.setMessage(Constants.OPERATION_NOK);
			messageTO.setIsError(true);
		}
		return messageTO;
	}

	@Override
	public MessageTO updateUser(User user) throws HibernateException {
		MessageTO messageTO = new MessageTO();
		if (!update(user)) {
			messageTO.setCode(Constants.CODE_ERROR_OPERATIONAL);
			messageTO.setMessage(Constants.OPERATION_NOK);
			messageTO.setIsError(true);
		}
		return messageTO;
	}


	public User getUserById(String id) throws HibernateException {
		return getObjectById(id);
	}

	@Override
	public List<User> getAllUser() throws WarningFunctionalException, ErrorFunctionalException, ErrorOperationalException, HibernateException {
		User user = new User();
		return getAllRows(user);
	}


}
